/*
 *  Copyright (C) <2009>  Pavel Vakeš <pavel.vakes@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config_dialog.h>


ConfigDialog::ConfigDialog() : QWidget()
{
	setupUi(this);

	// New
	connect(butAdd, SIGNAL( clicked() ), this, SLOT( createAccount() ));

	// Edit
	connect(butEdit, SIGNAL( clicked() ), this, SLOT( editAccount() ));
	connect(listAccounts, SIGNAL( itemDoubleClicked( QListWidgetItem * ) ), this, SLOT( editAccount() ));

	// Remove
	connect(butDelete, SIGNAL( clicked() ), this, SLOT( removeAccount() ));
}

ConfigDialog::~ConfigDialog()
{
	// Smazat listAccountItems
}

void ConfigDialog::loadAccounts(const std::vector<Account*> &accounts)
{
	for (unsigned int i = 0; i < accounts.size(); ++i) {
		new ListAccountItem(new Account(accounts[i]), listAccounts);
	}
}

void ConfigDialog::createAccount()
{
	ListAccountItem * account_item = new ListAccountItem(new Account());

	account_dialog = new AccountDialog(this, account_item);
	account_dialog->show();
}

void ConfigDialog::editAccount()
{
	if (listAccounts->currentItem()) {
		((ListAccountItem *) listAccounts->currentItem())->account->name;
		account_dialog = new AccountDialog(this, (ListAccountItem *) listAccounts->currentItem());
		account_dialog->show();
	}
}

void ConfigDialog::removeAccount()
{
	if (listAccounts->currentItem()) {
		delete listAccounts->item(listAccounts->currentRow());
	}
}

void ConfigDialog::closeAccountDialog()
{
	if (account_dialog->txtName->text().isEmpty()) {
		QMessageBox::critical(this, "Input error", tr2i18n("Name:") + " text field must be filled");
	}
	else {
		account_dialog->saveAccount();
		account_dialog->close();
	}
}
